
capture log close
log using $log/AED_analysis_03_heterogeneity, text replace


/* ------------------------------------------------------------------------------------ */
/* --- Estimation results Table 4  ---------------------------------------------------- */
/* ------------------------------------------------------------------------------------ */

clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off
   
#d;
global xvars " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last iza_zeit_la
				iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3
				iza_lnwage_tminus1 iza_lnwage_tminus2 iza_lnwage_tminus3 
				i.year i.entry_m";
				#d cr
				
				
#d;
global xvars_pred " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				iza_last_daily_income  
				iza_ende_last sv_last5 alo_last5
				i.year i.entry_m";
				#d cr				
	

	use  $orig/mobihilfen_all, clear
				gen share_relocation = (umzug)/(fahrkosten)
								gen year = jahr + 1
				keep ba share_relocation 
				tempfile intensity
				save `intensity'


		use $data/data_est_border, clear
		
		joinby ba year using `intensity', unmatched(master)
	tab _merge
	drop _merge
	
	sum share_relocation,d
	gen reg_rel =(share_relocation>r(p50))
		
	egen sv_last5 = rsum(iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3 iza_sv_tminus4 iza_sv_tminus5)
	egen alo_last5 = rsum(iza_alo_tminus1 iza_alo_tminus2 iza_alo_tminus3 iza_alo_tminus4 iza_alo_tminus5)
	
	gen intensity_mobi=exp(log_intensity_mobi)
	
	logit iza_umzug24 $xvars_pred if eintritt>=200506 & eintritt<=200705
	predict moving_prob
	margins, dydx(*)
	
	keep if eintritt<=200805 & eintritt>=200706
	
	
	qui sum moving_prob,d
	en group3=(moving_prob>r(p75))
gen group2=iza_sex	  
gen group1=rel_reg	
	
	
	
	#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	
	
	
	#d;
	global outc "month_sv1_24 
	earnings1_24	
	umzugskosten_24";
	#d cr

	bys pers_id: gen in_sample = (_n==1)
	
		
		forvalues group = 1/3 {
		    
			if (`group'==1|`group'==3) {
		 #d;
		 global cov " 
		c.vac_rate c.aloq   
				c.share_agriculture_avg c.share_industry_avg c.share_service_avg c.wanderungen_avg c.bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				c.iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last c.iza_zeit_la
				c.iza_sv_tminus1 c.iza_sv_tminus2 c.iza_sv_tminus3
				c.iza_lnwage_tminus1 c.iza_lnwage_tminus2 c.iza_lnwage_tminus3 
				i.year i.entry_m";
	   #d cr
		}
		
		if (`group'==2) {
		
		 #d;
		 global cov "	c.vac_rate c.aloq   
				c.share_agriculture_avg c.share_industry_avg c.share_service_avg c.wanderungen_avg c.bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				c.iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last c.iza_zeit_la
				c.iza_sv_tminus1 c.iza_sv_tminus2 c.iza_sv_tminus3
				c.iza_lnwage_tminus1 c.iza_lnwage_tminus2 c.iza_lnwage_tminus3 
				i.year i.entry_m";
	  
	   #d cr
		}
		
			
			
			if (`group'==1) {
		local name "LEA_reloc"
		local name1 "Commuting"
		local name2 "Relocation"
		}
		
		if (`group'==2) {
		local name "gender"
		local name1 "Men"
		local name2 "Women"
		}
		
		if (`group'==3) {
		local name "exp_reloc"
		local name1 "Not top quartile"
		local name2 "Top quartile"
		}
		tostring group`group', replace
		gen region_pair_int = substr(group`group',1,1) + substr(region_pair,1,6)
		destring group`group', replace
	
		gen log_intensity_int = log_intensity_alt*group`group'
	
		encode region_pair_int, g(region_pair2)
	
	foreach var in $outc {
	    
		qui reg `lhs' log_intensity_alt $cov  pair_*  [pw=1/fweight_reg] if group`group'==0, cluster(ba)
		
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		count if e(sample)==1 & in_sample==1
		matrix n=r(N) 
		matrix d1=b[1..1,1..1],se,n
		
		
		qui reg `lhs' log_intensity_alt $cov  pair_*  [pw=1/fweight_reg] if group`group'==1, cluster(ba)
		
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		count if e(sample)==1 & in_sample==1
		matrix n=r(N) 
		matrix d2=b[1..1,1..1],se,n
		
		
		qui reg `lhs'  log_intensity_alt log_intensity_int group`group' (group`group')##($cov pair_*) [pw=1/fweight_reg], cluster(ba) 
		 matrix r=r(table)
		 scalar p_int = r[4,2]
		 
		 matrix d_`lhs' = d1,d2,p_int
		
		
		
	}
	
	


#d;	
matrix d_sa`sa' = d_month_sv1_24\d_earnings1_24\d_umzugskosten_24
;
#d cr

matrix rownames d = 
	
	"Months employed"  
	"Earnings" 
	"Receiving relocation assistance" 
	;
	#delimit cr

matrix colname d = "group 1" "SE" "Obs" "group 2" "SE" "Obs" "P-value"
	di `name'
	mat list d
	
	drop region_pair_int log_intensity_int region_pair2 
}/*end group*/



log close
